<?php

class asynchronous_CronController extends Zend_Controller_Action {

	function init() {
		header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
		header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
		$this->getHelper('viewRenderer')->setNoRender();
	}

	//create a new job into the list of jobs
	function createnewjobAction() {
		require_once('asynchronous/objects/cronjob.php');
		try{		
			$jobId = asynchronous_Object_Cronjob::add($this->_getAllParams());
			$job = new asynchronous_Object_Cronjob($jobId);
		} catch(Status_Object_Exception $e) {
	      $data['status'] = 	'KO';
	      $data['error']['num'] = 	$e->getCode();
	      $data['error']['message'] = 	$e->getMessage();
	      echo Zend_Registry::get('dt_serializer')->serialize($data);	
	      return;
	    }
		$data['status'] = 	'OK';
		$data['nowUnixTimestamp'] = time();
		$data['nowISO8601'] = date('c',$data['nowUnixTimestamp']);
		$data['cronjob'] =  $job->asArray();

		echo Zend_Registry::get('dt_serializer')->serialize($data);
	    return;	  		
	} //end createnewjob
	
	//list all the jobs
	function listjobsAction() {
		require_once('asynchronous/objects/cronjob.php');
		
		try{
		  $jobs = asynchronous_Object_Cronjob::getAllJobs();
	    } catch(Asynchronous_Object_Exception $e) {
	      $data['status'] = 	'KO';
	      $data['error']['num'] = 	$e->getCode();
	      $data['error']['message'] = 	$e->getMessage();
	      echo Zend_Registry::get('dt_serializer')->serialize($data);	
	      return;
	    }

		$data['status'] = 	'OK';
		$data['nowUnixTimestamp'] = time();
		$data['nowISO8601'] = date('c',$data['nowUnixTimestamp']);
		
		if(!empty($jobs)) {
			foreach($jobs as $num => $jobObject) $data['cronjob'][] =  $jobObject->asArray();	
		}

		echo Zend_Registry::get('dt_serializer')->serialize($data);
	    return;			
	}

	//get the details of a job
	function getjobbyidAction() {
		require_once('asynchronous/objects/cronjob.php');
		
		try{
		  $job = new asynchronous_Object_Cronjob($this->_getParam('id'));
	    } catch(Asynchronous_Object_Exception $e) {
	      $data['status'] = 	'KO';
	      $data['error']['num'] = 	$e->getCode();
	      $data['error']['message'] = 	$e->getMessage();
	      echo Zend_Registry::get('dt_serializer')->serialize($data);	
	      return;
	    }

		$data['status'] = 	'OK';
		$data['nowUnixTimestamp'] = time();
		$data['nowISO8601'] = date('c',$data['nowUnixTimestamp']);
		$data['cronjob'] =  $job->asArray();

		echo Zend_Registry::get('dt_serializer')->serialize($data);
	    return;	    
		
		//
	}
	
	//get the list of jobs that are not schedule and put them in the job run table so they can be ran
	function publishjobstorunAction() {
		try {
			require_once('asynchronous/objects/cronjob.php');
			$JobsPublishedJobIdRunId = asynchronous_Object_Cronjob::publishJobsToRun();
	    } catch(Asynchronous_Object_Exception $e) {
		
		}
		$nbJobsPublished = count($JobsPublishedJobIdRunId);
Zend_Registry::get('dt_log')->log($nbJobsPublished.' Jobs published: '.serialize($JobsPublishedJobIdRunId),Zend_Log::NOTICE);
	
		$data['status'] = 	'OK';
		$data['nbJobsPublished'] = $nbJobsPublished;
		echo Zend_Registry::get('dt_serializer')->serialize($data);				
		return;
	
	} //end publishjobstorun

	
	
	
	//Process the next message in the queue (usually called by outside like front end or daemon job)
	function processAction() {
/*
		Zend_Registry::get('dt_log')->log('asynchronous message processing called',Zend_Log::INFO);
		//We retrieve the queue
		$queue = Zend_Registry::get('dt_asynchronousQueue');
		$messages = $queue->receive(1);
		if(count($messages) == 0) {
			Zend_Registry::get('dt_log')->log('No asynchronous message to proceed',Zend_Log::INFO);
			return;
		}
		foreach($messages as $i => $message) {
			Zend_Registry::get('dt_log')->log('adding asynchronous message to the stack:'.$message->getModule().'-'.$message->getController().'-'.$message->getAction(),Zend_Log::INFO);
			$this->_helper->actionStack($message->getAction(), $message->getController(),$message->getModule(), $message->getParams());
			$queue->deleteMessage($message);
		}
		//save the queue in the registry
*/		
		
	} //end processaction
  
} //End class
